package com.itboye.mengwu.app.ui.roof;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.adapter.BaoShuiQuAdapter;
import com.itboye.mengwu.app.ui.main.GoodsDetailActivity;
import com.itboye.mengwu.data.model.GoodsListModel;
import com.itboye.mengwu.data.model.GoodsListModel.GoodsList;
import com.itboye.mengwu.data.presenter.AddGoodsPresenter;
import com.itboye.mengwu.ui.shop.FlowLayout;
import com.itboye.mengwu.widget.PopupWindows;
import com.itboye.mengwu.widget.XListView;
import com.itboye.mengwu.widget.XListView.IXListViewListener;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

public class ActivityBaoShuiQu extends BaseActivity implements Observer,
		IXListViewListener, OnClickListener {
	private ImageView back;
	private TextView txt_title;
	private BaoShuiQuAdapter adapter;
	private XListView xlistview;
	private AddGoodsPresenter addGoodsPresenter;
	private GoodsListModel goodsListModel = new GoodsListModel();
	@SuppressWarnings("unused")
	private String uid;
	int pageNo = 1;

	private PopupWindows windows;
	// 价格区间
	private static final int ORDERCOUNTXT = 10;

	private static final int XIAOLIANGPAIXU = 11;

	private static final int PACIEDI_ISGAO = 12;

	private static final int PACIEGAO_ISDI = 13;

	public static String order;
	/**
	 * 根据自己输入价格区间进行筛选
	 */
	private String max_cost;
	private String min_const;

	private PopupWindow morePopupWindow;

	/***
	 * 区间
	 */

	public static ImageView imgBaoSui;
	public static TextView renqi, xiaoliang, jianggequjian, txtAddUp,
			txtAddDow;

	private EditText tditextJiaGeDi;
	private EditText tditextJiaGeGao;
	private ArrayList<GoodsList> arraylist = new ArrayList<GoodsListModel.GoodsList>();
	String str[] = { "打折", "促销", "一折", "优惠", "特价", "抽奖" };

	TextView[] childView;

	int posion = 0;
	FlowLayout addpupowindowflowlayout;

	@Override
	public int initView() {
		
		return R.layout.activity_baosuiqu;
	}

	@Override
	public void onMyCreate() {
		String num = getIntent().getStringExtra("special");
		txt_title.setText(num);
		adapter = new BaoShuiQuAdapter(arraylist, this);
		xlistview.setAdapter(adapter);
		xlistview.setXListViewListener(this);
		xlistview.setPullLoadEnable(true);
		xlistview.setPullRefreshEnable(true);
		showProgressDialog("数据加载中...", true);
		goodsListModel = new GoodsListModel();
		addGoodsPresenter = new AddGoodsPresenter(this);
		order = ORDERCOUNTXT + "";
		goodsListModel.setOrder(order);
		addGoodsPresenter.getInfo(goodsListModel);
		inxlistview();
		txt_title.setText("保税区");
		adapter = new BaoShuiQuAdapter(arraylist, this);
		xlistview.setAdapter(adapter);
		imgBaoSui.setBackgroundResource(R.drawable.down_gray);
		uid = (String) SPUtils.get(this, null, SpPublicParams.UID, "");

		inxlistview();
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				finish();
			}
		});
	}

	public void inxlistview() {
		xlistview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				Intent intent = new Intent(ActivityBaoShuiQu.this,
						GoodsDetailActivity.class);
				intent.putExtra("pid", arraylist.get(position - 1).getId());
				startActivity(intent);
			}
		});
	}

	public void initer() {
		renqi = (TextView) findViewById(R.id.renqi);
		xiaoliang = (TextView) findViewById(R.id.xiaoliang);
		// xinpin = (TextView) findViewById(R.id.xinpin);
		jianggequjian = (TextView) findViewById(R.id.jianggequjian);
		imgBaoSui = (ImageView) findViewById(R.id.imgBaoSui);
		imgBaoSui.setVisibility(View.GONE);
	}

	@Override
	public void update(Observable observable, Object data) {
		
		ResultEntity entity = handlerError(data);
		if (entity != null) {

			if (entity.getEventType() == AddGoodsPresenter.AddGoods_success) {
				arraylist.clear();
				arraylist.addAll(((GoodsListModel) entity.getData()).getList());

				adapter.notifyDataSetChanged();
			}

		}
		try {
			closeProgressDialog();
		} catch (Exception e) {

		}
		stopLoad();
		//
	}

	@Override
	public void onRefresh() {
		
		pageNo = 1;
		addGoodsPresenter.getInfo(goodsListModel);
	}

	@Override
	public void onLoadMore() {
		
		pageNo++;
		addGoodsPresenter.getInfo(goodsListModel);
	}

	public void stopLoad() {
		xlistview.stopLoadMore();
		xlistview.stopRefresh();
	}

	@SuppressWarnings("deprecation")
	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.renqi:
			// if (true) {
			// imgBaoSui.setBackgroundResource(R.drawable.up);
			// } else {
			// imgBaoSui.setBackgroundResource(R.drawable.down_gray);
			// }
			// oninter();
			onZoneHePaiXu(v);

			renqi.setTextColor(getResources().getColor(R.color.main_color));
			xiaoliang.setTextColor(getResources().getColor(R.color.black));
			// xinpin.setTextColor(getResources().getColor(R.color.black));
			jianggequjian.setTextColor(getResources().getColor(R.color.black));
			break;
		case R.id.xiaoliang:
			renqi.setTextColor(getResources().getColor(R.color.black));
			xiaoliang.setTextColor(getResources().getColor(R.color.main_color));
			jianggequjian.setTextColor(getResources().getColor(R.color.black));
			goodsListModel.setOrder(XIAOLIANGPAIXU + "");
			addGoodsPresenter.getInfo(goodsListModel);
			break;
		case R.id.xinpin:
			renqi.setTextColor(getResources().getColor(R.color.black));
			xiaoliang.setTextColor(getResources().getColor(R.color.black));
			// xinpin.setTextColor(getResources().getColor(R.color.main_color));
			jianggequjian.setTextColor(getResources().getColor(R.color.black));
			break;
		case R.id.jianggequjian:
			onFna();
			renqi.setTextColor(getResources().getColor(R.color.black));
			xiaoliang.setTextColor(getResources().getColor(R.color.black));
			// xinpin.setTextColor(getResources().getColor(R.color.black));
			jianggequjian.setTextColor(getResources().getColor(
					R.color.main_color));
			break;

		default:
			break;
		}
	}

	@SuppressWarnings("deprecation")
	public void onZoneHePaiXu(View v) {
		View view = LayoutInflater.from(this).inflate(
				R.layout.more_popup_dialog, null);
//		int h = this.getWindowManager().getDefaultDisplay().getHeight();
		int w = this.getWindowManager().getDefaultDisplay().getWidth();
		morePopupWindow = new PopupWindow(view, w,
				android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
		morePopupWindow.setBackgroundDrawable(new BitmapDrawable());
		morePopupWindow.setFocusable(true);
		morePopupWindow.setOutsideTouchable(true);
		morePopupWindow.showAsDropDown(v);
		// 刷新状态
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0000000000);
		// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		morePopupWindow.setBackgroundDrawable(dw);

		LinearLayout linZonghe = (LinearLayout) view
				.findViewById(R.id.linZonghe);

		LinearLayout linJiageGao = (LinearLayout) view
				.findViewById(R.id.linJiageGao);
		LinearLayout linJiageDi = (LinearLayout) view
				.findViewById(R.id.linJiageDi);
		linZonghe.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				renqi.setText("综合排序");
				goodsListModel.setOrder(ORDERCOUNTXT + "");
				addGoodsPresenter.getInfo(goodsListModel);
				morePopupWindow.dismiss();
			}
		});

		linJiageGao.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				renqi.setText("价格由高到低");
				goodsListModel.setOrder(PACIEGAO_ISDI + "");
				addGoodsPresenter.getInfo(goodsListModel);
				morePopupWindow.dismiss();
			}
		});

		linJiageDi.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				renqi.setText("价格由低到高");
				goodsListModel.setOrder(PACIEDI_ISGAO + "");
				addGoodsPresenter.getInfo(goodsListModel);
				morePopupWindow.dismiss();
			}
		});
	}

	@SuppressWarnings("deprecation")
	public void onFna() {

		View view = View.inflate(this, R.layout.add_popup_dialog, null);
		windows = new PopupWindows(this, jianggequjian, view);
		tditextJiaGeDi = (EditText) view.findViewById(R.id.tditextJiaGeDi);
		tditextJiaGeGao = (EditText) view.findViewById(R.id.editextJiaGeGao);

		Button btn_finsh = (Button) view.findViewById(R.id.btn_finsh);

		childView = new TextView[str.length];//<TextView>();

		for (int i = 0; i < str.length; i++) {
			posion = i;
			addpupowindowflowlayout = (FlowLayout) view.findViewById(R.id.addpupowindowflowlayout);
			View vieww = LayoutInflater.from(this).inflate(R.layout.item_addppuwindow,
					null);
			TextView textView = (TextView) vieww.findViewById(R.id.txtviewadd);
			textView.setText(str[i]);

			// Log.d("hu", arrayList.get(i).getName());
			textView.setGravity(Gravity.CENTER);
			textView.setTextColor(getResources().getColor(R.color.black));
			textView.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.biaoqianshape2));
			textView.setTag(str[i]);
			textView.setTag(R.id.tag_second, i);
//			childView.add(textView);
			android.widget.RelativeLayout.LayoutParams lP = new android.widget.RelativeLayout.LayoutParams(
					android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
					android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
			lP.setMargins(10, 10, 10, 10);
			addpupowindowflowlayout.addView(vieww,lP);
//			childView[i].setOnClickListener(new OnClickListener() {
//
//				@Override
//				public void onClick(View v) {
//					
//					ByAlert.alert(childView[Integer.parseInt(v.getTag() + "")]
//							.getText().toString());
//				}
//			});
		}
		btn_finsh.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				windows.dismiss();
			}
		});

		Button btnCongZhi = (Button) view.findViewById(R.id.btnCongZhi);
		Button btnQueren = (Button) view.findViewById(R.id.btnQueren);
		btnQueren.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				min_const = tditextJiaGeDi.getText().toString().trim();
				max_cost = tditextJiaGeGao.getText().toString().trim();
				if (min_const == null || min_const == "" || max_cost == null
						|| max_cost == "") {
					ByAlert.alert("请输入价格区间");
				} else {
					goodsListModel.setMax_cost(max_cost);
					goodsListModel.setMin_cost(min_const);
					addGoodsPresenter.getInfo(goodsListModel);
				}
				windows.dismiss();
			}
		});

		btnCongZhi.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				onChongZhi();
			}
		});

	}

	// public void onJiageShuaiXuan(View view){
	//
	// }

	public void onChongZhi() {
		tditextJiaGeDi.setText("");
		tditextJiaGeGao.setText("");
	}

}
